package com.seveneight.baijiesteward.ui.main.activity;
import android.content.Intent;
import android.support.annotation.NonNull;
import android.view.View;
import android.widget.TextView;
import com.afollestad.materialdialogs.DialogAction;
import com.afollestad.materialdialogs.MaterialDialog;
import com.alibaba.fastjson.JSON;
import com.seveneight.baijiesteward.R;
import com.seveneight.baijiesteward.adapter.PlanListInfoAdapter;
import com.seveneight.baijiesteward.base.BaseActivity;
import com.seveneight.baijiesteward.bean.AnyBean;
import com.seveneight.baijiesteward.bean.PlanDetailListInfoBean;
import com.seveneight.baijiesteward.http.MyCallBack;
import com.seveneight.baijiesteward.service.DownLoadService;
import com.seveneight.baijiesteward.ui.user.model.PlanRePayInfoModel;
import com.seveneight.baijiesteward.ui.user.model.PlanRePayRemoveModel;
import com.seveneight.baijiesteward.utils.MyListView;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import butterknife.BindView;
import butterknife.OnClick;

/**
 * 计划还款详情
 */
public class RePayPlanInfoActivity extends BaseActivity implements View.OnClickListener{
    @BindView(R.id.tv_title)
    TextView tv_title;
    @BindView(R.id.recharge_amount)
    TextView recharge_amount;
    @BindView(R.id.repay_amount)
    TextView repay_amount;
    @BindView(R.id.service_charge)
    TextView service_charge;
    @BindView(R.id.bt_submit)
    TextView bt_submit;

    String id="";
    int type=0;

    List<PlanDetailListInfoBean> listDate=new ArrayList<>();
    PlanListInfoAdapter mAdapter;
    MyListView listView;
    PlanRePayInfoModel api=new PlanRePayInfoModel();
    PlanRePayRemoveModel apiRemove=new PlanRePayRemoveModel();

    @Override
    protected void initLayout() {
        setContentView(R.layout.activity_re_pay_plan_info);
        if (getIntent().hasExtra("id")){
            id=getIntent().getStringExtra("id");
            type=getIntent().getIntExtra("type",0);
        }
    }

    @Override
    protected void initView() {
        bt_submit.setVisibility(type==1?View.VISIBLE:View.GONE);
        tv_title.setText("计划详情");
        listView=findViewById(R.id.repay_list);
        mAdapter=new PlanListInfoAdapter(this,listDate);
        listView.setAdapter(mAdapter);
        mAdapter.notifyDataSetChanged();
    }

    @Override
    protected void initData() {
        Map<String,String> params=new HashMap<>();
        params.put("RepayPlanId",id);
        api.getBankList(this, jsonFormat.AddCard(params), new MyCallBack<AnyBean>() {
            @Override
            public void onSuccess(AnyBean anyBean) {
                recharge_amount.setText("充值总金额:  "+anyBean.getRechargeAmount());
                repay_amount.setText("还款总金额:  "+anyBean.getRepayAmount());
                service_charge.setText("交易总手续费:  "+anyBean.getServiceCharge());
                listDate= JSON.parseArray(anyBean.getPlanDetailList(),PlanDetailListInfoBean.class);
                mAdapter.refresh(listDate);
            }

            @Override
            public void onFailed() {

            }
        });

    }

    @Override
    protected void setListener() {

    }
    @OnClick({R.id.iv_back,R.id.bt_submit})
    @Override
    public void onClick(View view){
        switch (view.getId()){
            case R.id.iv_back:
                finish();
                break;
            case R.id.bt_submit:
//                AlertDialog.Builder builder=new AlertDialog.Builder(this);
//                builder.setMessage("是否取消执行");
//                builder.setNeutralButton("取消", null);
//                builder.setNegativeButton("确定", new DialogInterface.OnClickListener() {
//                    @Override
//                    public void onClick(DialogInterface dialogInterface, int i) {
//                        remove();
//                    }
//                });
//                builder.show();

                new MaterialDialog.Builder(this)
                        .content("是否取消执行")
                        .contentColorRes(R.color.graytext)
                        .backgroundColorRes(R.color.white)
                        .negativeColorRes(R.color.mainblue)
                        .negativeText(R.string.negative)
                        .positiveColorRes(R.color.mainblue)
                        .positiveText(R.string.positive)
                        .onPositive(new MaterialDialog.SingleButtonCallback() {
                            @Override
                            public void onClick(@NonNull MaterialDialog dialog, @NonNull DialogAction which) {
                                dialog.dismiss();
                                remove();
                            }
                        })
                        .build().show();
                break;
        }
    }
    public void remove(){
        Map<String,String> params=new HashMap<>();
        params.put("RepayPlanId",id);
        apiRemove.getBankList(this, jsonFormat.AddCard(params), new MyCallBack<AnyBean>() {
            @Override
            public void onSuccess(AnyBean anyBean) {
                setResult(RESULT_OK,new Intent());
                finish();
            }

            @Override
            public void onFailed() {

            }
        });

    }
}
